<%-- 
    Document   : post
    Created on : 2014-9-5, 11:59:23
    Author     : chensy
--%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" session="false" %>
<%@ include file="/WEB-INF/jspf/import.jspf" %>
<!doctype HTML>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
        <title> 车友会 </title>
        <meta name="keywords" content="" />
        <meta name="description" content="http://js.3conline.com/wap/pcauto/2014/" />
        <meta name="author" content="zhoubo_gz sunbiao_gz" />
        <link rel="stylesheet" type="text/css" href="http://js.3conline.com/wap/pcauto/2014/cheyouhui/css/fbdt.css">
    </head>
    <body>
        <script>document.body.insertBefore(document.createElement("script"),document.body.firstChild).src="http://count.pcauto.com.cn/count.php?channel=-99999&screen="+screen.width+"*"+screen.height+"&refer="+encodeURIComponent(document.referrer)+"&anticache="+new Date().getTime()+"&url="+encodeURIComponent(location.href);</script>

        <div class="g-doc body">
            <!-- 公共头部 -->
            <%@include file="../common/intf6007.html" %>
            <!-- 公共头部end -->

            <div class="header-bg">
                <a href="javascript:history.go(-1);" class="header-back"></a>
                <span class="header-title">发表动态</span>
            </div>
            <dl class="m-fbdt">
                <div class="input-area">
                    <textarea placeholder="说点什么吧..." id="content"></textarea>
                    <div class="pic-upload" id="picWrap">
                        <div class="picfile-wrap" id="picfileWrap"><input type="file" name="picfile0" id="picfile0" class="picfile" onchange="changeEvent(this)" accept="image/*"></div>
                    </div>
                </div>
                <input id="dtSubmit" type="button" value="发表" class="btn-submit" onclick="submitForm();">
            </dl>
        </div>
        <script class="defer" src="http://js.3conline.com/wap/pcauto/2013/bbs/bbs_core.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript" src="http://js.3conline.com/wap/2013/common/js/zepto.min.js"></script>
        <script type="text/javascript" src="http://js.3conline.com/wap/common/Location-min.js"></script>
    </body>
    <script>
        // 初始化定位
        var localProCode = 0, localCityCode = 0;
        if("${resultType}"=="1"){//从同城首页发动态，会把相应的省市ID传过来
            localProCode = "${provinceId}";
            localCityCode = "${cityId}";
        } else {//从车友会发动态，主动获取省市ID
            Location.init(function(location) {
                localProCode = location.proCode;
                localCityCode = location.cityCode;
            });
        }
   		
        //+++++++++++多图上传js+++++++++++
        var imgData = []; //图片数组
        var picWrap = document.getElementById('picWrap'); //图片外壳
        var picfileWrap = document.getElementById('picfileWrap'); //上传控件外壳
        var idNum = 0; //控件id值
        var uploadNum = 0; //上传中的图片数量
        //创建不同name和id值的上传空间
        function createInput() {
            idNum++;
            var inputEle = document.createElement("input");
            inputEle.setAttribute("onchange", "changeEvent(this)");
            inputEle.className = 'picfile';
            inputEle.type = 'file';
            inputEle.accept = 'image/*'; //只限定图片
            inputEle.id = "picfile" + idNum;
            inputEle.name = "picfile" + idNum;
            picfileWrap.appendChild(inputEle);
        }

        //每次选择完图片上传 , 同时隐藏这个上传控件 , 创建下一个不同id和name值的控件,这样做是为了能够实现多图片同时上传
        function changeEvent(obj) {
            if (!obj) {
                return;
            }
            obj.style.display = "none";
            doUpload(obj.id);
            createInput();
        }

        function doUpload(fileId) {
            $("#dtSubmit").attr("disabled",'');
            UPC.init({
                localProxy: '/favicon.ico',
                upcPath: "${UPCROOT}",
                command: [53003, 53004, 53016, 53017,53018,53019,53020],
                application: "autoclub"
            });
            uploadNum++;
            UPC.post({
                url: "/upload_quick.jsp",
                data: {
                    rotate: 0,
                    keepSrc: 'true',
                    readExif: 'yes'
                },
                file: [fileId],
                complete: function (json) {
                    if (json.retCode == 0) {
                        if(!picWrap.querySelector("#imgDiv"+fileId)){//上传一半取消
                            return;
                        }
                        picWrap.querySelector("#imgDiv"+fileId).innerHTML = '<img src="' + json.files[0].url + '" />'; //传图成功插入图片
                        var index = 0;
                        for(var i = 0;i<json.files.length;i++){
                            if(json.files[i].url.indexOf("1024x1024")!=-1){
                                index=i;
                            }
                        }
                        var obj = new Object();
                        obj.url = json.files[index].url;
                        obj.width = json.files[index].width;
                        obj.height = json.files[index].height;
                        imgData.push(obj); 
                    } else {
                        core.showTopTip("上传图片失败");
                        core.hideTopTip();
                        picWrap.removeChild(picWrap.querySelector("#imgDiv"+fileId));
                    }
                    upcComleteSetBtn();
                }
            });
            //插入loading外壳
            var divEle = document.createElement("div");
            divEle.className = 'item';
            divEle.id= 'imgDiv'+fileId
            picWrap.insertBefore(divEle, picfileWrap);
        }
        
        function upcComleteSetBtn(){
            uploadNum--;
            if(uploadNum<1){
                $("#dtSubmit").removeAttr("disabled");
            }
        }

        
        // 绑定图片删除事件
        $(picWrap).on('click', '.item', function() {
            imgData.splice($('.item',picWrap).index(this), 1);
            picWrap.removeChild(this);
            upcComleteSetBtn();
        });
        function appendImg(imgData) {
            var str = '\r\n';
            for (var i = 0; i < imgData.length; i++) {
                str += '[img]';
                str += imgData[i];
                str += '[/img]\r\n';
            }
            return str;
        }
        var isSend = 0;
        function submitForm(){
            var content = $("#content").val();
            var imagesjson = "[";
            if(content.length>300){
                core.showTopTip("回复内容长度不能超过300");
                core.hideTopTip();
                return false;
            }
            for(var i = 0; i < imgData.length; i++){
                imagesjson+= "{\"url\":\""+imgData[i].url+"\",\"seq\":"+(i+1)+",width:"+imgData[i].width+",height:"+imgData[i].height+"}";
            } 
            imagesjson +="]";
            if(isSend == 1){
                return;
            }
            isSend = 1;
            $("#dtSubmit").val("发表中...").attr("disabled","");
            $.ajax({
                url:"${WAPROOT}/dyna/createCommentDyna.do",
                dataType:"json",
                data:{
                    content:content,
                    images:imagesjson,
                    clubId:"${clubId}",
                    provinceId:localProCode,
                    cityId:localCityCode,
                    fromType:1
                },
                type:"post",
                success:function(data){
                    isSend = 0;
                    if(data.code==0){
                        core.showTopTip("发表成功");
                        if("${resultType}"=="1"){
                            location.href="${WAPROOT}/${resultUri}/";
                        }else{
                            location.href="${WAPROOT}/${myUrl}/";
                        }
                    }else{
                        core.showTopTip(data.message);
                        core.hideTopTip();
                    }
                    $("#dtSubmit").val("发表")
                    $("#dtSubmit").removeAttr("disabled","");
                },
                error:function(xmlHttpRequest, textStatus, errorThrown){
                    isSend = 0;
                     $("#dtSubmit").val("发表")
                    $("#dtSubmit").removeAttr("disabled","");
                    var message = "网络错误,请联系管理员";
                    if(xmlHttpRequest.status==503){
                        var json = eval("("+xmlHttpRequest.responseText+")");
                        message = (json.message == ""?"网络错误,请联系管理员":json.message);
                    }
                    core.showTopTip(message);
                    core.hideTopTip();
                }
            });
        }
    </script>
</html>